import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author gao
 * @date 2025/9/5 13:24
 */

class ListNode {
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public ListNode removeZeroSumSublists(ListNode head) {
        // 虚拟头节点
        ListNode dummy = new ListNode(0);
        dummy.next = head;

        // 哈希表记录前缀和对应的节点
        Map<Integer, ListNode> prefixSumMap = new HashMap<>();
        prefixSumMap.put(0, dummy);

        int prefixSum = 0;
        ListNode cur = head;

        // 记录所有前缀和
        while (cur != null) {
            prefixSum += cur.val;
            prefixSumMap.put(prefixSum, cur);
            cur = cur.next;
        }

        // 重置前缀和当前节点
        prefixSum = 0;
        cur = dummy;

        // 第二次遍历，删除和为0的连续节点
        while (cur != null) {
            prefixSum += cur.val;
            // 如果当前前缀和存在于哈希表中，跳过中间节点
            cur.next = prefixSumMap.get(prefixSum).next;
            cur = cur.next;
        }
        return dummy.next;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            ListNode head = null;
            ListNode tail = null;

            for (int i = 0; i < n; i++) {
                int val = in.nextInt();
                ListNode node = new ListNode(val);
                if (head == null) {
                    head = node;
                    tail = node;
                } else {
                    tail.next = node;
                    tail = tail.next;
                }
            }

            Solution solution = new Solution();
            print(solution.removeZeroSumSublists(head));
        }
    }

    private static void print(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}